package aishua.com.mytestapplist.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import aishua.com.mytestapplist.R;
import aishua.com.mytestapplist.bean.ItemBean;
import aishua.com.mytestapplist.utils.LogUtils;

/**
 * Created by aishua on 2018/3/27.
 */

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyRecycleViewHoler> {
    private static final String TAG = "RecycleViewAdapter-";
    private Context mContext;
    private ArrayList<ItemBean> dataList;

    public RecycleViewAdapter(Context mContext,ArrayList<ItemBean> dataList){
        this.mContext = mContext;
        this.dataList = dataList;
    }

    @Override
    public MyRecycleViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
         View view = View.inflate(mContext, R.layout.itemview_item,null);

        LogUtils.v(TAG+":onCreateViewHolder()"+"-----view:"+view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(mContext,"测试Recyleview的条目点击",Toast.LENGTH_SHORT).show();
            }
        });

        return new MyRecycleViewHoler(view);
    }

    @Override
    public void onBindViewHolder(MyRecycleViewHoler holder, final int position) {
        LogUtils.v(TAG+":onBindViewHolder()");
         holder.dispalyView(position);
         holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onRecycleViewClickListener.onRecycleViewClick();
            }
        });
         holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
             @Override
             public boolean onLongClick(View v) {

                 onRccycleViewLongClickListener.onRccycleViewLongClick();

                 return false;
             }
         });
    }

    @Override
    public int getItemCount() {

        return dataList.size();
    }

    public class MyRecycleViewHoler extends RecyclerView.ViewHolder{

        private ImageView fruit_img;
        private TextView fruit_name;
        public MyRecycleViewHoler(View itemView) {
            super(itemView);
             fruit_name = (TextView) itemView.findViewById(R.id.fruit_name);
             fruit_img = (ImageView) itemView.findViewById(R.id.fruit_image);
        }

         public void  dispalyView(int position){

            fruit_img.setBackgroundResource(dataList.get(position).getFruitImg());
            fruit_name.setText(dataList.get(position).getFruitName());

         }

    }
    public void setonRecycleViewClickListener(onRecycleViewClickListener onRccycleViewLongClickListener){
        this.onRecycleViewClickListener =onRccycleViewLongClickListener;
    }
    public void setOnRccycleViewLongClickListener(onRccycleViewLongClickListener onRccycleViewLongClickListener){

        this.onRccycleViewLongClickListener = onRccycleViewLongClickListener;
    }

    private onRecycleViewClickListener onRecycleViewClickListener;
    private onRccycleViewLongClickListener onRccycleViewLongClickListener;
    public interface onRecycleViewClickListener{

        public void onRecycleViewClick();
    }
    public interface  onRccycleViewLongClickListener{

        public void onRccycleViewLongClick();
    }
}
